summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/platform/PlatformGamesFragment.kt
blob: 42b61a81a4d5972b5ef0b138a7b8a35de0e3f68c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

package org.yuzu.yuzu_emu.ui.platform

import android.database.Cursor
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver.OnGlobalLayoutListener
import android.widget.TextView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import org.yuzu.yuzu_emu.R
import org.yuzu.yuzu_emu.YuzuApplication
import org.yuzu.yuzu_emu.adapters.GameAdapter
import org.yuzu.yuzu_emu.utils.InsetsHelper

class PlatformGamesFragment : Fragment(), PlatformGamesView {
    private val presenter = PlatformGamesPresenter(this)
    private var adapter: GameAdapter? = null
    private lateinit var recyclerView: RecyclerView
    private lateinit var textView: TextView

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val rootView = inflater.inflate(R.layout.fragment_grid, container, false)
        findViews(rootView)
        presenter.onCreateView()
        return rootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        adapter = GameAdapter()

        // Organize our grid layout based on the current view.
        if (isAdded) {
            view.viewTreeObserver
                .addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
                    override fun onGlobalLayout() {
                        if (view.measuredWidth == 0) {
                            return
                        }
                        var columns = view.measuredWidth /
                                requireContext().resources.getDimensionPixelSize(R.dimen.card_width)
                        if (columns == 0) {
                            columns = 1
                        }
                        view.viewTreeObserver.removeOnGlobalLayoutListener(this)
                        val layoutManager = GridLayoutManager(activity, columns)
                        recyclerView.layoutManager = layoutManager
                        recyclerView.adapter = adapter
                    }
                })
        }

        // Add swipe down to refresh gesture
        val pullToRefresh = view.findViewById<SwipeRefreshLayout>(R.id.swipe_refresh)
        pullToRefresh.setOnRefreshListener {
            refresh()
            pullToRefresh.isRefreshing = false
        }

        setInsets()
    }

    override fun refresh() {
        val databaseHelper = YuzuApplication.databaseHelper
        databaseHelper!!.scanLibrary(databaseHelper.writableDatabase)
        presenter.refresh()
        updateTextView()
    }

    override fun showGames(games: Cursor) {
        if (adapter != null) {
            adapter!!.swapCursor(games)
        }
        updateTextView()
    }

    private fun updateTextView() {
        textView.visibility =
            if (adapter!!.itemCount == 0) View.VISIBLE else View.GONE
    }

    private fun findViews(root: View) {
        recyclerView = root.findViewById(R.id.grid_games)
        textView = root.findViewById(R.id.gamelist_empty_text)
    }

    private fun setInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(recyclerView) { view: View, windowInsets: WindowInsetsCompat ->
            val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
            view.updatePadding(bottom = insets.bottom)
            windowInsets
        }
    }

    companion object {
        const val TAG = "PlatformGamesFragment"
    }
}